﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class PFSHForm : Form
    {
        int regID, avpID, farmID, pfshID;
        tb_school pfsh;
        bool insert = true;
        public PFSHForm()
        {
            InitializeComponent();
        }

        private void PFSHForm_Load(object sender, EventArgs e)
        {
            if (!InformationClass.isAdmin)
            {
                regID = InformationClass.regionID;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.DisplayMember = "Name";
                cbAVP.ValueMember = "ID";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
                cbRegions.Enabled = false;
                cbAVP.Enabled = true;
            }
            else
            {

                cbRegions.SelectedIndexChanged -= cbRegilons_SelectedIndexChanged;
                cbRegions.DataSource = UtilClass.FillCBRegions();
                cbRegions.DisplayMember = "Name";
                cbRegions.ValueMember = "ID";
                cbRegions.SelectedIndex = -1;
                cbRegions.Text = string.Empty;
                cbRegions.SelectedIndexChanged += new EventHandler(cbRegilons_SelectedIndexChanged);
            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cbAVP.SelectedIndex >= 0)
            {
                cbFH.DataSource = null;
                dgvPFSh.DataSource = null;

                cbFH.SelectedIndexChanged -= cbFH_SelectedIndexChanged;
                avpID = Convert.ToInt32(cbAVP.SelectedValue);

                cbFH.DataSource = DBClass.Farms.Where(wh => wh.AVPID == avpID)
                    .Select(se => new { Name = se.Name, ID = se.ID })
                    .OrderBy(o=>o.Name)
                    .ToList();
                cbFH.ValueMember = "ID";
                cbFH.DisplayMember = "Name";
                cbFH.Enabled = true;
                cbFH.SelectedIndex = -1;
                cbFH.Text = string.Empty;
                cbFH.SelectedIndexChanged += new EventHandler(cbFH_SelectedIndexChanged);
            }
            else
                avpID = 0;

        }

        private void cbRegilons_SelectedIndexChanged(object sender, EventArgs e)
        {
            dgvPFSh.DataSource = null;
            if (cbRegions.SelectedIndex >= 0)
            {
                cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
                regID = Convert.ToInt32(cbRegions.SelectedValue);
                cbAVP.Enabled = true;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.DisplayMember = "Name";
                cbAVP.ValueMember = "ID";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
                cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
            }
            else
                regID = 0;
        }

        private void cbFH_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cbFH.SelectedIndex >= 0)
            {

                farmID = Convert.ToInt32(cbFH.SelectedValue);
                dgvPFSh.DataSource = DBClass.Schools.Where(wh => wh.FarmID == farmID).Select(se => new { ID = se.ID, pfshName = se.Name, Area = se.Area }).ToList();
                CancelEditPFShButton.Enabled = true;
                GridSettings();
            }
            else
                farmID = 0;
        }

        private void GridSettings()
        {
            if (dgvPFSh.DataSource != null)
            {
                dgvPFSh.Columns["ID"].Visible = false;
                if (InformationClass.Lang)
                {
                    dgvPFSh.Columns["pfshName"].HeaderText = "ПФШ";
                    dgvPFSh.Columns["Area"].HeaderText = "Площадь";
                }
                else
                {
                    dgvPFSh.Columns["pfshName"].HeaderText = "ДФМ";
                    dgvPFSh.Columns["Area"].HeaderText = "Майдон";
                }
            }
        }

        private void AddPFShButton_Click(object sender, EventArgs e)
        {
            insert = true;
            pfsh = null;
            tbPFSH.Enabled = tbArea.Enabled = InsertButtob.Enabled = true;
            InsertButtob.Text = "Добавить";
        }

        private void InsertButtob_Click(object sender, EventArgs e)
        {
            decimal area;
            if (farmID == 0 || String.IsNullOrEmpty(tbPFSH.Text) || String.IsNullOrEmpty(tbArea.Text))
            {
                MessageBox.Show("Заполенны не все поля.");
                return;
            }
            else
            {
                if (tbArea.Text.GetDecimalOrDefault(out area))
                {
                    if (insert && pfsh == null)
                    {
                        if (string.IsNullOrEmpty(tbPFSH.Text) || DBClass.Schools.Where(wh => wh.Name == tbPFSH.Text && wh.FarmID == farmID).Select(se => se.Name).ToArray().Contains(tbPFSH.Text))
                        {
                            MessageBox.Show(msgClass.msgUncorrectData, msgClass.msgAchtung);
                            tbPFSH.Focus();
                            tbPFSH.SelectionStart = 0;
                            tbPFSH.SelectionLength = tbPFSH.Text.Length;
                            return;
                        }

                        pfsh = new tb_school
                        {
                            Area = area,
                            FarmID = farmID,
                            Name = tbPFSH.Text
                        };
                        DBClass.Schools.InsertOnSubmit(pfsh);

                    }
                    else if (pfsh != null)
                    {
                        pfsh.Area = area;
                        pfsh.Name = tbPFSH.Text;
                    }
                    else
                        return;
                    try
                    {
                        DBClass.RESPContext.SubmitChanges();
                        MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung); 
                        cbAVP.Enabled = cbFH.Enabled = cbRegions.Enabled = true;
                        dgvPFSh.DataSource = DBClass.Schools.Where(wh => wh.FarmID == farmID).Select(se => new { ID = se.ID, pfshName = se.Name, Area = se.Area }).ToList();
                        CancelEditPFShButton.Enabled = true;
                        GridSettings();
                    }
                    catch
                    {
                        MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                    }

                }
                else
                {
                    MessageBox.Show("Не верно заполненна площадь");
                }

            }
            tbPFSH.Text = tbArea.Text = string.Empty;
            tbPFSH.Enabled = tbArea.Enabled = InsertButtob.Enabled = false;
            pfsh = null;
        }

        private void CancelEditPFShButton_Click(object sender, EventArgs e)
        {
            tbPFSH.Text = tbArea.Text = string.Empty;
            tbPFSH.Enabled = tbArea.Enabled = InsertButtob.Enabled = false;
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            if (dgvPFSh.RowCount > 0 && dgvPFSh.CurrentRow != null)
            {
                insert = false;
                InsertButtob.Text = "Исправить";
                pfshID = (int)dgvPFSh["ID", dgvPFSh.CurrentRow.Index].Value;
                pfsh = DBClass.Schools.Where(wh => wh.ID == pfshID).SingleOrDefault();
                if (pfsh != null)
                {
                    tbPFSH.Enabled = tbArea.Enabled = InsertButtob.Enabled = true;
                    tbPFSH.Text = pfsh.Name;
                    tbArea.Text = pfsh.Area.Value.ToString();

                    cbAVP.Enabled = cbFH.Enabled = cbRegions.Enabled = false;
                }
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (dgvPFSh.RowCount > 0 && dgvPFSh.CurrentRow != null)
            {
                pfshID = (int)dgvPFSh["ID", dgvPFSh.CurrentRow.Index].Value;
                pfsh = DBClass.Schools.Where(wh => wh.ID == pfshID).SingleOrDefault();
                if (pfsh != null)
                {
                    if (MessageBox.Show(msgClass.msgDeleteQuestion, msgClass.msgAchtung, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        try
                        {
                            DBClass.Schools.DeleteOnSubmit(pfsh);
                            DBClass.RESPContext.SubmitChanges();

                            dgvPFSh.DataSource = DBClass.Schools.Where(wh => wh.FarmID == farmID).Select(se => new { ID = se.ID, pfshName = se.Name, Area = se.Area }).ToList();
                            CancelEditPFShButton.Enabled = true;
                            GridSettings();
                        }
                        catch
                        {
                            MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                        }
                    }
                }
            }
        }
    }
}
